package consumer;

import queue.Queue1;


/**
 * @author Xu,
 * @data 2020/12/19，
 * @time 21:35
 * 消费者判断生成的随机数是否素数；
 */
public class Consumer implements Runnable{
    private int count = 0;
    private Queue1 queue;
    private int perNum;
    public Consumer(Queue1 queue1, int perNum){
        this.queue = queue1;
        this.perNum = perNum;
    }

    public void isPrime(Object q){
        boolean flag1=true;
        long n = (long)q;
        if(n ==1 || n %2 ==0 && n !=2 )
        {
            flag1 = false;
        }
        else
        {
            for( int i =3; i< Math.sqrt(n); i+=2)
            {
                if( n % i == 0)
                {
                    flag1 = false;
                    break;
                }
            }
        }
    }

    @Override
    public void run(){
        for(int i=0; i<perNum; i++){
            isPrime(queue.getTake());
            //System.out.println("消费者doing");
            count++;
        }
    }

    public int getCount() {
        return count;
    }
}
